<template>
	<div class="system-dic-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-input size="default" placeholder="请输入版本号" style="max-width: 180px" v-model="state.tableData.param.param.versions.val"> </el-input>
				<el-button size="default" type="primary" @click="seachClick">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success"  v-auth="'/system/queue/add'" @click="onOpenAdd('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增版本
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column prop="id" label="ID" show-overflow-tooltip width="80px"></el-table-column>
				<el-table-column prop="versions" label="版本号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="log" label="版本内容" show-overflow-tooltip></el-table-column>
				
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<el-button size="small" text type="primary" v-auth="'/system/queue/edit'" @click="onOpenEdit('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" v-auth="'/system/queue/del'" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.page"
				background
				v-model:page-size="state.tableData.param.limit"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<AddEditDialog ref="addEditDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup name="versions">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';

import { systemVersionsApi } from '/@/api/system/versions/index';
// 引入组件
const AddEditDialog = defineAsyncComponent(() => import('./dialog.vue'));
const paramOp = {
	versions:'%%'
}
// 定义变量内容
const addEditDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			param:{
				versions:{},
			},
			page: 1,
			limit: 10,
		},
	},
});

// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	for (let key in state.tableData.param.param) {
		if (state.tableData.param.param[key].val && state.tableData.param.param[key].val !== '') {
			state.tableData.param.param[key].op = paramOp[key] || '='
		}else{
			state.tableData.param.param[key] = {}
		}
	}
	systemVersionsApi().index(state.tableData.param).then(res=>{
		state.tableData.data = res.data.list;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
	})
};
// 打开新增弹窗
const onOpenAdd = (type) => {
	addEditDialogRef.value.openDialog(type);
};
// 打开修改弹窗
const onOpenEdit = (type, row) => {
	addEditDialogRef.value.openDialog(type, row);
};
// 删除
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除该版本信息：“${row.title}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
			systemVersionsApi().del({ids:row.id}).then(res=>{
                getTableData();
                ElMessage.success('删除成功');
            })
	}).catch(() => {});
};

const seachClick = ()=>{
	onHandleCurrentChange(1)
}
// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.limit = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	console.log(12);
	state.tableData.param.page = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
